using System;
using System.Collections.Generic;
using System.Text;

namespace PersistenceObject.Framework.Expressions
{
    public class DecimalField : FieldAdapter
    {
        public DecimalField(string name) : base(name)
        {
        }

        public DecimalField(string name, FieldAdapter basefield) : base(name, basefield)
        {
        }

        public DecimalField As(string name)
        {
            return new DecimalField(string.Format("{0} {1}", this.Name, name), this);
        }

        public DecimalField At(Table table)
        {
            String[] tempArr = this.Name.Split('.');
            if (tempArr.Length > 1)
            {
                this.name = tempArr[1];
            }
            string alias = table.Alias;
            if (string.IsNullOrEmpty(alias))
            {
                alias = table.Name;
            }
            return new DecimalField(string.Format("{0}.{1}", alias, this.Name), this);
        }

        public Expression Between(decimal lvalue, decimal rvalue)
        {
            return Expressions.Between(base.Name, lvalue, rvalue);
        }

        public Expression In(params decimal[] value)
        {
            return Expressions.In(base.Name, value);
        }

        public Expression In(Table table, Expression exp)
        {
            return this.In(table, this, exp);
        }

        public Expression In(Table table, DecimalField field, Expression exp)
        {
            Expression expression = new InTableFieldExpression(this.Name, table.Name, field.Name);
            expression.Add(new IExpression[] { exp });
            return expression;
        }

        public Expression NotBetween(decimal lvalue, decimal rvalue)
        {
            return Expressions.Between(this.Name, lvalue, rvalue, true);
        }

        public Expression NotIn(params decimal[] value)
        {
            return Expressions.In(this.Name, value, true);
        }

        public Expression NotIn(Table table, Expression exp)
        {
            return this.NotIn(table, this, exp);
        }

        public Expression NotIn(Table table, DecimalField field, Expression exp)
        {
            InTableFieldExpression expression = new InTableFieldExpression(base.Name, table.Name, field.Name);
            expression.Add(new IExpression[] { exp });
            expression.IsNotIn = true;
            return expression;
        }

        public static DoubleField operator +(DecimalField field, DecimalField field1)
        {
            return new DoubleField(string.Format("{0}+{1}", field.Name, field1.Name), field);
        }

        public static DecimalField operator +(DecimalField field, DoubleField field1)
        {
            return new DecimalField(string.Format("{0}+{1}", field.Name, field1.Name), field);
        }

        public static DecimalField operator +(DecimalField field, IntField field1)
        {
            return new DecimalField(string.Format("{0}+{1}", field.Name, field1.Name), field);
        }

        public static DecimalField operator /(DecimalField field, DoubleField field1)
        {
            return new DecimalField(string.Format("{0}/{1}", field.Name, field1.Name), field);
        }

        public static DecimalField operator /(DecimalField field, IntField field1)
        {
            return new DecimalField(string.Format("{0}/{1}", field.Name, field1.Name), field);
        }

        public static DoubleField operator /(DoubleField field, DecimalField field1)
        {
            return new DoubleField(string.Format("{0}/{1}", field.Name, field1.Name), field);
        }

        public static Expression operator ==(DecimalField field, decimal value)
        {
            return Expressions.Equals(field.Name, value);
        }

        public static Expression operator >(DecimalField field, decimal value)
        {
            return Expressions.GreaterThan(field.Name, value);
        }

        public static Expression operator >=(DecimalField field, decimal value)
        {
            return Expressions.GreaterThanOrEquals(field.Name, value);
        }

        public static Expression operator !=(DecimalField field, decimal value)
        {
            return Expressions.NotEquals(field.Name, value);
        }

        public static Expression operator <(DecimalField field, decimal value)
        {
            return Expressions.LessThan(field.Name, value);
        }

        public static Expression operator <=(DecimalField field, decimal value)
        {
            return Expressions.LessThanOrEquals(field.Name, value);
        }

        public static DecimalField operator *(DecimalField field, DoubleField field1)
        {
            return new DecimalField(string.Format("{0}*{1}", field.Name, field1.Name), field);
        }

        public static DecimalField operator *(DecimalField field, IntField field1)
        {
            return new DecimalField(string.Format("{0}*{1}", field.Name, field1.Name), field);
        }

        public static DoubleField operator *(DoubleField field, DecimalField field1)
        {
            return new DoubleField(string.Format("{0}*{1}", field.Name, field1.Name), field);
        }

        public static DecimalField operator -(DecimalField field, DoubleField field1)
        {
            return new DecimalField(string.Format("{0}-{1}", field.Name, field1.Name), field);
        }

        public static DecimalField operator -(DecimalField field, IntField field1)
        {
            return new DecimalField(string.Format("{0}-{1}", field.Name, field1.Name), field);
        }

        public static DoubleField operator -(DoubleField field, DecimalField field1)
        {
            return new DoubleField(string.Format("{0}-{1}", field.Name, field1.Name), field);
        }

        public FieldValue Set(decimal value)
        {
            return new FieldValue(this.Name, value);
        }

    }
}

